﻿CREATE FUNCTION [dbo].[udf_List2Table]
(
	@List VARCHAR(MAX),
	@List1 VARCHAR(MAX),
	@Delim CHAR
)
RETURNS
@ParsedList TABLE
(
	item VARCHAR(MAX),
	item1 VARCHAR(MAX)
)
AS
BEGIN
	DECLARE @item VARCHAR(MAX), @item1 VARCHAR(MAX), @Pos INT, @Pos1 INT
	SET @List = LTRIM(RTRIM(@List))+ @Delim
	SET @List1 = LTRIM(RTRIM(@List1))+ @Delim
	SET @Pos = CHARINDEX(@Delim, @List, 1)
	SET @Pos1 = CHARINDEX(@Delim, @List1, 1)
	WHILE @Pos > 0
	BEGIN
		SET @item = LTRIM(RTRIM(LEFT(@List, @Pos - 1)))
		SET @item1 = LTRIM(RTRIM(LEFT(@List1, @Pos1 - 1)))
		IF @item <> ''
		BEGIN
			INSERT INTO @ParsedList (item, item1)
			VALUES (CAST(@item AS INT),CAST(@item1 AS INT))
		END
		SET @List = RIGHT(@List, LEN(@List) - @Pos)
		SET @List1 = RIGHT(@List1, LEN(@List1) - @Pos1)
		SET @Pos = CHARINDEX(@Delim, @List, 1)
		SET @Pos1 = CHARINDEX(@Delim, @List1, 1)
	END
	RETURN
END


